package com.content.view
{
	import com.clerkq.Tracing.fp10.Tr;
	import com.clerkq.Tracing.fp10.TrConsole;
	import com.content.AppFacade;
	import com.content.model.GlobalVarProxy;
	import com.content.model.LoadedAssets.ConfigProxy;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.Frame.Content.Content;
	import com.content.view.Frame.Content.ContentMediator;
	import com.content.view.Share.Player.AudioPlayer_Mediator;
	import com.content.view.Share.Player.BookPlayer_Mediator;
	import com.content.view.Share.Player.VideoPlayer_Mediator;
	import com.content.view.Share.Progress.Progress;
	import com.content.view.Share.Progress.ProgressMediator;
	import com.library.effect.Rippler;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.system.Capabilities;
	
	import org.aswing.AsWingManager;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.utilities.startupmanager.model.StartupMonitorProxy;
	
	public class AppMediator extends Mediator implements IMediator
	{
		public static const NAME:String				= 'AppMediator';
		
		private var _content:Sprite;
		
		public function AppMediator(viewComponent:Object=null)
		{
			super( NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			// 注册“系统初始化进度条”
			facade.registerMediator( new ProgressMediator( ParentView ) );
			
			// Add Resize before loading 
			ParentView.stage.addEventListener(Event.RESIZE, OnResize);
			ParentView.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				StartupMonitorProxy.LOADING_PROGRESS,
				StartupMonitorProxy.LOAD_RESOURCE_TIMED_OUT,
				StartupMonitorProxy.LOADING_COMPLETE,
				StartupMonitorProxy.LOADING_FINISHED_INCOMPLETE,
				StartupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED,
				StartupMonitorProxy.LOAD_RESOURCES_REJECTED,
				
				App.RESIZE_MANUAL
			];
		}
		
		override public function handleNotification(note:INotification):void
		{
			var name:String = note.getName();
			var body:Object = note.getBody();
			
			switch ( name )
			{
				case StartupMonitorProxy.LOAD_RESOURCES_REJECTED:
				case StartupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED:
					trace( "Abnormal State, Abort" );
					break;
				case StartupMonitorProxy.LOADING_PROGRESS:
					var perc:int = int(note.getBody() as Number);
					trace( "Loading Progress: " + perc + "%" );
					sendNotification( Progress.UPDATE, { percent: perc } );
					break;
				case StartupMonitorProxy.LOADING_COMPLETE:
					trace( ">>> Loading Complete" );
					initializeView();
					break;
				case StartupMonitorProxy.LOADING_FINISHED_INCOMPLETE:
					trace( "Loading Finished Incomplete" );
					break;
				
				case App.RESIZE_MANUAL:
					OnResize(null);
					break;
			}
		}
		
		private function initializeView():void
		{
			
			//ASWing初始化
			AsWingManager.initAsStandard(ParentView);
			//facade.registerProxy( new SocketProxy());
			
			// Content Wrapper
			facade.registerMediator( new ContentMediator( ParentView ) );
			
			
			// create the TrConsole instance and add it to the stage
			var console:TrConsole = new TrConsole(ParentView.stage);
			Tr.ace(Capabilities.version);
			
			facade.registerMediator( new VideoPlayer_Mediator( ParentView ) );
			facade.registerMediator( new AudioPlayer_Mediator( ParentView ) );
			facade.registerMediator( new BookPlayer_Mediator( ParentView ) );
			//facade.registerMediator( new MsgBoxMediator( ParentView ) );
			
			// 系统“加载”及“初始化”完成
			sendNotification( AppFacade.APP_INIT);
		}
		
		/* Event Handler
		* ---------------------------------------------------- */
		private function OnResize(evt:Event):void{
			if(ParentView.stage){
				var sWidth:int = ParentView.stage.stageWidth;
				var sHeight:int = ParentView.stage.stageHeight;
				sendNotification( App.RESIZE_SYS, {width: sWidth, height:  sHeight});
			}
		}
		
		private function OnMouseUp(evt:MouseEvent):void{
			if(ParentView.stage){
				sendNotification( App.MOUSEUP, {evt: evt});
			}
		}
		
		
		/* Properties
		 * ----------------------------------------------------------------------- */
		private function get ParentView():Sprite						{return viewComponent as Sprite;}
		private function get Proxy_GlobalVar():GlobalVarProxy	{return facade.retrieveProxy( GlobalVarProxy.NAME ) as GlobalVarProxy;}
		private function get Proxy_Config():ConfigProxy			{return facade.retrieveProxy( ConfigProxy.NAME ) as ConfigProxy;}
		
	}
}